<?php
class View_Admin_TemplateManangement extends View_Admin_Index_Template {
    public $oTemp;
    public function displayManagerIndex($oModel) {
        $this->initTemplate($oModel);
        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN .
            'html/template_management_index.html');
        $this->oTemp->assign('pageHeding', $oModel->aLanguage[MODULE_HEADING]['template']);
        /**
         * toolbar setup
         */
        $arr = $oModel->dogetAllRecord();
        if(count($arr) >= MAX_TEMPLATES)
            $this->oTemp->assign('toobar_new', 'javascript: alert(\''.$oModel->aLanguage['TEMPLATE']['max_template_alert'].'\')');
        else
            $this->oTemp->assign('toobar_new', '?sMod=template_index&sAction=add_new_form');
        $this->oTemp->parse('main.toobar_new');
        $this->oTemp->assign('max_tpl', MAX_TEMPLATES);
    }

    public function displayListTemplate($oModel) {
        $arr = $oModel->dogetAllRecord();
        
        $a = array();
        $i = 0;
        if($arr) {
            $set_default = false;
            foreach($arr as $ke=>$value) {

                $a[$i] = $value['id'];
                $i++;
                if($value['status']==1 && $value['active_dedault']==1) {

                    $set_default = true;
                }
            }
            $min = min($a);

            if(!$set_default) {
                $oModel->setData('id', $min);
                $oModel->setData('status', 1);
                $oModel->setData('active_dedault', 1);
                $oModel->updateTemplate();
            }
        }
        $rs = $oModel->getListTemplate();
        if (is_array($rs)) {
            $i = 0;
            foreach ($rs as $row) {
                $i++;
                $row['no'] = $i;
                $row['css'] = "row" . ($i % 2);
                 if ($row['status'] == 1) {

                    $row['status'] = 'tick.png';
                }else {
                    $row['status'] = 'publish_x.png';
                }

                if ($row['active_dedault'] == 1) {
                    $row['active_dedault'] = 'default';
                }else {
                    $row['active_dedault'] = 'none';
                }
                $row['name'] = preview_text($row['name'],
                    50);
                if ($_SESSION['category_change_igm_id'] != '' && $_SESSION['category_change_igm_id'] ==
                    $row['product_category_id']) {

                    $category_image_old = $row['product_category_image'];
                    $oModel->deleteThumbImage($category_image_old);
                }


                $this->oTemp->assign('rowCategory', $row);
                $this->oTemp->parse('main.rowCategory');
            }
        }
        else {
            $this->oTemp->parse('main.no_found');
        }
        
        $this->oTemp->assign( 'navigator', $oModel->oNavigator->navigator() );
    }

    /**
     * ADD NEW CATEGORY
     */
    public function displayNewTemplateIndex($oModel) {
        $this->initTemplate($oModel);

        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN .
            'html/template_mamagement_new.html');
        $this->oTemp->assign_file('temp_editor', PATH_TEMPLATE_ADMIN .
            'html/temp_editor_basic.html');
        $this->oTemp->assign('pageHeding', $oModel->aLanguage[MODULE_HEADING]['template']);
        $this->oTemp->assign('max_tpl', MAX_TEMPLATES);
        $arr = $oModel->dogetAllRecord();
        if(count($arr) >= MAX_TEMPLATES){
            echo '<script type="text/javascript">';
            echo 'alert("'.$oModel->aLanguage['TEMPLATE']['max_template_alert'].'");';
            echo 'location.href = "?sMod=template_index";';
            echo '</script>';
        }
        /**
         * toolbar setup
         */
        $this->oTemp->assign('status', 'checked');
        $this->oTemp->assign('toobar_save', 'check_update_template()');
        $this->oTemp->parse('main.toobar_save');
        $this->oTemp->assign('toobar_cancel', '?sMod=template_index');
        $this->oTemp->parse('main.toobar_cancel');

        $this->oTemp->assign('explanation', $oModel->getData('default_explanation'));
        $this->oTemp->assign('damage', $oModel->getData('default_damage'));
        $this->oTemp->assign('dagame', $oModel->getData('default_dagame'));
        $this->oTemp->assign('rank', $oModel->getData('default_rank'));
        $this->oTemp->assign('notes', $oModel->getData('default_notes'));
        $this->oTemp->assign('size', $oModel->getData('default_size'));
        
    }
    
    public function displayBoxCategoryParent($oModel) {
        $rsSiteMap = $oModel->getListCategoryParentChild();
        if (is_array($rsSiteMap)) {
            $i = 0;
            foreach ($rsSiteMap as $rowSiteMap) {
                $i++;
                $rowSiteMap['no'] = $i;
                if ($rowSiteMap['product_category_id'] == $oModel->getData('product_category_parent_id')) {
                    $rowSiteMap['selected'] = 'selected';
                }
                $rowSiteMap['prefix'] = $this->displayTreePrefix($rowSiteMap['category_level']);
                $this->oTemp->assign('rowCatParent', $rowSiteMap);
                $this->oTemp->parse('main.rowCatParent');
            }
        }
    }
    private function displayTreePrefix($level) {
        $sPrefix = "|";
        for ($i = 1; $i < $level; $i++) {
            $sPrefix .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|";
        }
        $sPrefix .= "____";
        return $sPrefix;
    }
    private function displayTreePrefixImg($level) {
    //$sPrefix = "|";
        if ($level == 1)
            $sPrefix = "<img src='{URL_TEMPLATE_ADMIN}image/tree_img/folder.gif'/>";
        else {
            for ($i = 1; $i < $level; $i++) {
                $sPrefix .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            $sPrefix .= "<img src='{URL_TEMPLATE_ADMIN}image/tree_img/child.gif'/>";
        }
        //$sPrefix .= "____";*/
        //else
        //$sPrefix= URL_TEMPLATE_ADMIN.'image/tree_img/child.gif';
        return $sPrefix;
    }
    /**
     * EDIT USER
     */
    public function displayEditTemplateIndex($oModel) {
        $this->initTemplate($oModel);
        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN .
            'html/template_mamagement_new.html');
        $this->oTemp->assign_file('temp_editor', PATH_TEMPLATE_ADMIN .
            'html/temp_editor_basic.html');
        $this->oTemp->assign('pageHeding', $oModel->aLanguage[MODULE_HEADING]['template']);
        $this->oTemp->assign( 'id', $oModel->getData('id') );
        /**
         * toolbar setup
         */
        $this->oTemp->assign('toobar_save', 'check_update_template()');
        $this->oTemp->parse('main.toobar_save');
        $this->oTemp->assign('toobar_cancel', '?sMod=template_index');
        $this->oTemp->parse('main.toobar_cancel');
         
        $this->oTemp->assign('explanation', $oModel->getData('default_explanation'));
        $this->oTemp->assign('damage', $oModel->getData('default_damage'));
        $this->oTemp->assign('dagame', $oModel->getData('default_dagame'));
        $this->oTemp->assign('rank', $oModel->getData('default_rank'));
        $this->oTemp->assign('notes', $oModel->getData('default_notes'));
        $this->oTemp->assign('size', $oModel->getData('default_size'));
    }

    public function displayInfoTem($rowManager) {
        if ($rowManager['status'] == 1) {
            $rowManager['status_checked'] = 'checked="checked"';
        }else {
            $rowManager['status'] = "";
            $rowManager['disabled'] = 'disabled="disabled"';
        }
        if ($rowManager['active_dedault'] == 1) {
            $rowManager['active_default_checked'] = 'checked="checked"';

        }else {
            $rowManager['active_default'] = "";
        }
        $rowManager['id_old'] = $rowManager['id'];
        $this->assignExtract($rowManager);
    }

    public function resultXML($oModel, $cCMD, $cResult) {
        $oJSON = new Services_JSON();
        echo $oJSON->encode(array('HEAD' => array('CMD' => $cCMD, 'DATA' => date('d/m/Y H:m:s'), ),
        'BODY' => array('RES' => $cResult)));
    }

    /*************************************************************************************************
    CATEGORY CHOOSE
    *************************************************************************************************/
    public function displatChooseCategoryIndex($oModel) {
        $this->initTemplate($oModel);
        $this->oTemp->assign_file('tempMainContent', PATH_TEMPLATE_ADMIN .
            'html/category_choose.html');

        $this->oTemp->assign('pageHeding', $oModel->aLanguage['MENU']['choose_category']);

        /**
         * toolbar setup
         */

        $this->oTemp->assign('toobar_save', 'check_form()');
        $this->oTemp->parse('main.toobar_save');
        $this->oTemp->assign('toobar_cancel', '?sMod=category_index');
        $this->oTemp->parse('main.toobar_cancel');
    }

    public function displayFirstListCat($oModel) {
    //	$rs=$oModel->getListParentCategory();
    //     	if(is_array($rs))
    //     	{
    //     		foreach($rs as $row)
    //     		{
    //     			$rs_child=$oModel->getListChildOnly($row['product_category_id']);
    //     			if(is_array($rs_child))
    //     			{
    //     				$row['parent_img']=URL_TEMPLATE_ADMIN."image/arrow.gif";
    //     			}
    //     			else
    //     			$row['parent_img']="";
    //     			$row['highlight']= "HighLightTR(this,'td_1')";
    //     			$this->oTemp->assign('RowCat',$row);
    //				$this->oTemp->parse('main.ResultCat.RowCat');
    //     		}
    //     		$this->oTemp->assign('row_id','td_1');
    //     		$this->oTemp->parse('main.ResultCat');
    //     	}
    }

    public function ajax_displayListParentCategory($oModel) {

        $rs = $oModel->getListParentCategory();
        if (is_array($rs)) {
            $num_row = 0;
            foreach ($rs as $row) {
                $rs_child = $oModel->getListChildOnly($row['product_category_id']);
                if (is_array($rs_child)) {
                    $row['parent_img'] = URL_TEMPLATE_ADMIN . "image/arrow.gif";
                }
                else
                    $row['parent_img'] = "";
                $row['highlight'] = "";
                $row['row_id'] = 0;
                $row['num_row'] = "parent" . $num_row;

                $this->oTemp->assign('i',$num_row%2);
                $num_row++;

                $this->oTemp->assign('RowCat', $row);
                $this->oTemp->parse('main.ResultCat.RowCat');
            }
            $this->oTemp->assign('row_id', 0);
            $this->oTemp->parse('main.ResultCat');
        }
        return $this->oTemp->text('main.ResultCat');
    }

    public function ajax_displayListChildCateogry($oModel, $parent_id) {
        $rs = $oModel->getListChildOnly($parent_id);
        $current_level = $oModel->getData('current_level');
        $next_level = $current_level + 1;
        //$_SESSION['test_count']=$next_level;

        if (is_array($rs)) {
            $num_row=0;
            foreach ($rs as $row) {
                $rs_child = $oModel->getListChildOnly($row['product_category_id']);

                if (is_array($rs_child)) {
                    $row['parent_img'] = URL_TEMPLATE_ADMIN . "image/arrow.gif";
                }
                else
                    $row['parent_img'] = "";

                $row['num_row'] = $next_level."_child" . $num_row;
                $num_row++;

                $row['row_id'] = $next_level;

                $this->oTemp->assign('i',$num_row%2);
                $num_row++;

                $this->oTemp->assign('RowCat', $row);
                $this->oTemp->parse('main.ResultCat.RowCat');
            }
            $this->oTemp->assign('row_id', $next_level);
            $this->oTemp->parse('main.ResultCat');
        }
        return $this->oTemp->text('main.ResultCat');
    }
}
